// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.characters;

import zombie.Lua.LuaHookManager;
import zombie.ai.states.SwipeStatePlayer;
import zombie.characters.Moodles.MoodleType;
import zombie.characters.skills.PerkFactory;
import zombie.inventory.InventoryItem;
import zombie.inventory.InventoryItemFactory;
import zombie.inventory.types.HandWeapon;
import zombie.iso.IsoCell;
import zombie.iso.IsoMovingObject;
import zombie.iso.Vector2;
import zombie.network.packets.hit.AttackVars;
import zombie.ui.UIManager;

public class IsoLivingCharacter extends IsoGameCharacter {
    public float useChargeDelta = 0.0F;
    public final HandWeapon bareHands;
    public boolean bDoShove = false;
    public boolean bCollidedWithPushable = false;
    public IsoGameCharacter targetOnGround;

    public IsoLivingCharacter(IsoCell cell, float x, float y, float z) {
        super(cell, x, y, z);
        this.bareHands = (HandWeapon)InventoryItemFactory.CreateItem("Base.BareHands");
    }

    public boolean isCollidedWithPushableThisFrame() {
        return this.bCollidedWithPushable;
    }

    public boolean AttemptAttack(float ChargeDelta) {
        Object object = null;
        if (!(this.leftHandItem instanceof HandWeapon handWeapon)) {
            handWeapon = this.bareHands;
        }

        if (handWeapon != this.bareHands && this instanceof IsoPlayer) {
            AttackVars attackVars = new AttackVars();
            SwipeStatePlayer.instance().CalcAttackVars(this, attackVars);
            this.setDoShove(attackVars.bDoShove);
            if (LuaHookManager.TriggerHook("Attack", this, ChargeDelta, handWeapon)) {
                return false;
            }
        }

        return this.DoAttack(ChargeDelta);
    }

    public boolean DoAttack(float ChargeDelta) {
        if (this.isDead()) {
            return false;
        } else {
            if (this.leftHandItem != null) {
                InventoryItem inventoryItem = this.leftHandItem;
                if (inventoryItem instanceof HandWeapon) {
                    this.useHandWeapon = (HandWeapon)inventoryItem;
                    if (this.useHandWeapon.getCondition() <= 0) {
                        return false;
                    }

                    int int0 = this.Moodles.getMoodleLevel(MoodleType.Endurance);
                    if (this.useHandWeapon.isCantAttackWithLowestEndurance() && int0 == 4) {
                        return false;
                    }

                    int int1 = 0;
                    if (this.useHandWeapon.isRanged()) {
                        int int2 = this.useHandWeapon.getRecoilDelay();
                        Float _float = (float)int2 * (1.0F - (float)this.getPerkLevel(PerkFactory.Perks.Aiming) / 30.0F);
                        this.setRecoilDelay((float)_float.intValue());
                    }

                    if (this instanceof IsoSurvivor && this.useHandWeapon.isRanged() && int1 < this.useHandWeapon.getMaxHitCount()) {
                        for (int int3 = 0; int3 < this.getCell().getObjectList().size(); int3++) {
                            IsoMovingObject isoMovingObject = (IsoMovingObject)this.getCell().getObjectList().get(int3);
                            if (isoMovingObject != this
                                && isoMovingObject.isShootable()
                                && this.IsAttackRange(isoMovingObject.getX(), isoMovingObject.getY(), isoMovingObject.getZ())) {
                                float float0 = 1.0F;
                                if (float0 > 0.0F) {
                                    Vector2 vector20 = new Vector2(this.getX(), this.getY());
                                    Vector2 vector21 = new Vector2(isoMovingObject.getX(), isoMovingObject.getY());
                                    vector21.x = vector21.x - vector20.x;
                                    vector21.y = vector21.y - vector20.y;
                                    boolean _boolean = false;
                                    if (vector21.x == 0.0F && vector21.y == 0.0F) {
                                        _boolean = true;
                                    }

                                    Vector2 vector22 = this.getForwardDirection();
                                    this.DirectionFromVector(vector22);
                                    vector21.normalize();
                                    float float1 = vector21.dot(vector22);
                                    if (_boolean) {
                                        float1 = 1.0F;
                                    }

                                    if (float1 > 1.0F) {
                                        float1 = 1.0F;
                                    }

                                    if (float1 < -1.0F) {
                                        float1 = -1.0F;
                                    }

                                    if (float1 >= this.useHandWeapon.getMinAngle() && float1 <= this.useHandWeapon.getMaxAngle()) {
                                        int1++;
                                    }

                                    if (int1 >= this.useHandWeapon.getMaxHitCount()) {
                                        break;
                                    }
                                }
                            }
                        }
                    }

                    if (UIManager.getPicked() != null) {
                        this.attackTargetSquare = UIManager.getPicked().square;
                        if (UIManager.getPicked().tile instanceof IsoMovingObject) {
                            this.attackTargetSquare = ((IsoMovingObject)UIManager.getPicked().tile).getCurrentSquare();
                        }
                    }

                    if (this.useHandWeapon.getAmmoType() != null && !this.inventory.contains(this.useHandWeapon.getAmmoType())) {
                        return false;
                    }

                    if (this.useHandWeapon.getOtherHandRequire() == null
                        || this.rightHandItem != null && this.rightHandItem.getType().equals(this.useHandWeapon.getOtherHandRequire())) {
                        if (!this.useHandWeapon.isRanged()) {
                            this.getEmitter().playSound(this.useHandWeapon.getSwingSound(), this);
                            zombie.WorldSoundManager.instance
                                .addSound(
                                    this,
                                    (int)this.getX(),
                                    (int)this.getY(),
                                    (int)this.getZ(),
                                    this.useHandWeapon.getSoundRadius(),
                                    this.useHandWeapon.getSoundVolume()
                                );
                        }

                        this.AttackWasSuperAttack = this.superAttack;
                        this.changeState(SwipeStatePlayer.instance());
                        if (this.useHandWeapon.getAmmoType() != null) {
                            if (this instanceof IsoPlayer) {
                                IsoPlayer.getInstance().inventory.RemoveOneOf(this.useHandWeapon.getAmmoType());
                            } else {
                                this.inventory.RemoveOneOf(this.useHandWeapon.getAmmoType());
                            }
                        }

                        if (this.useHandWeapon.isUseSelf() && this.leftHandItem != null) {
                            this.leftHandItem.Use();
                        }

                        if (this.useHandWeapon.isOtherHandUse() && this.rightHandItem != null) {
                            this.rightHandItem.Use();
                        }

                        return true;
                    }

                    return false;
                }
            }

            return false;
        }
    }

    public boolean isDoShove() {
        return this.bDoShove;
    }

    public void setDoShove(boolean _bDoShove) {
        this.bDoShove = _bDoShove;
    }
}
